import 'dart:io';
import 'package:word_game/battle/battle.dart';
import 'package:word_game/engine/core.dart';
import 'package:yaml/yaml.dart';
import 'package:uuid/uuid.dart';

class MonsterManager extends Object {
  static MonsterManager instance = MonsterManager();

  Map<String, Monster> _objectMap={};

  appendObjectsFromYAML(String fileName) {
    var content = File(fileName).readAsStringSync();
    var yaml = loadYaml(content);
    var items = yaml['monsters'] as List;
    for (var item in items) {
      Monster obj;
      var type = item['type'];
      if (type=='MonsterCluster') {
        obj = MonsterCluster(item);
      } else if (type=='Monster') {
        obj = Monster(item);
      }
      
      _objectMap[obj.code] = obj;
    }
  }

  Monster objectWithCode(String code) {
    Monster template = _objectMap[code];
    Monster result;
    if (template is MonsterCluster) {
      result = MonsterCluster(null);
    } else {
      result = Monster(null);
    }
    result.copyData(template);
    result.id = Uuid().v4();
    return result;
  }

}